package com.example.springbootdemo.handle;

import com.example.springbootdemo.base.BaseResult;
import com.example.springbootdemo.exception.ApiException;
import com.example.springbootdemo.utils.ResultUtil;

import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class ExceptionHandle {

    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public BaseResult handle(Exception e) {
        if (e instanceof ApiException) {
            ApiException apiException = (ApiException) e;
            return ResultUtil.failed(apiException.getCode(), e.getMessage());
        }
        return ResultUtil.failed(-1, e.getMessage());
    }
}
